//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Managers;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Product category mapping manager
    /// </summary>
    public class ProductCategoryManager
    {
        #region Constants
        private const string PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY = "Nop.productcategory.allbycategoryid-{0}-{1}";
        private const string PRODUCTCATEGORIES_ALLBYPRODUCTID_KEY = "Nop.productcategory.allbyproductid-{0}-{1}";
        private const string PRODUCTCATEGORIES_BY_ID_KEY = "Nop.productcategory.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Deletes a product category mapping
        /// </summary>
        /// <param name="ProductCategoryID">Product category identifier</param>
        public static void DeleteProductCategory(int ProductCategoryID)
        {
            ProductCategoryDBManager.DeleteProductCategory(ProductCategoryID);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY);
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_ALLBYPRODUCTID_KEY);
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets product category mapping collection
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        /// <returns>Product a category mapping collection</returns>
        public static ProductCategoryCollection GetProductCategoriesByCategoryID(int CategoryID)
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY, showHidden, CategoryID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductCategoryCollection)obj2;
            }

            ProductCategoryCollection productCategoryCollection = ProductCategoryDBManager.GetProductCategoriesByCategoryID(CategoryID, showHidden);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productCategoryCollection);
            }
            return productCategoryCollection;
        }

        /// <summary>
        /// Gets a product category mapping collection
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product category mapping collection</returns>
        public static ProductCategoryCollection GetProductCategoriesByProductID(int ProductID)
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(PRODUCTCATEGORIES_ALLBYPRODUCTID_KEY, showHidden, ProductID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductCategoryCollection)obj2;
            }

            ProductCategoryCollection productCategoryCollection = ProductCategoryDBManager.GetProductCategoriesByProductID(ProductID, showHidden);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productCategoryCollection);
            }
            return productCategoryCollection;
        }

        /// <summary>
        /// Gets a product category mapping 
        /// </summary>
        /// <param name="ProductCategoryID">Product category mapping identifier</param>
        /// <returns>Product category mapping</returns>
        public static ProductCategory GetByProductCategoryID(int ProductCategoryID)
        {
            string key = string.Format(PRODUCTCATEGORIES_BY_ID_KEY, ProductCategoryID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductCategory)obj2;
            }

            ProductCategory productCategory = ProductCategoryDBManager.GetByProductCategoryID(ProductCategoryID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productCategory);
            }
            return productCategory;
        }

        /// <summary>
        /// Inserts a product category mapping
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="CategoryID">Category identifier</param>
        /// <param name="IsFeaturedProduct">A value indicating whether the product is featured</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product category mapping </returns>
        public static ProductCategory InsertProductCategory(int ProductID, int CategoryID,
           bool IsFeaturedProduct, int DisplayOrder)
        {
            ProductCategory productCategory = ProductCategoryDBManager.InsertProductCategory(ProductID, CategoryID,
            IsFeaturedProduct, DisplayOrder);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY);
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_ALLBYPRODUCTID_KEY);
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_BY_ID_KEY);
            }
            return productCategory;
        }

        /// <summary>
        /// Updates the product category mapping 
        /// </summary>
        /// <param name="ProductCategoryID">Product category mapping  identifier</param>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="CategoryID">Category identifier</param>
        /// <param name="IsFeaturedProduct">A value indicating whether the product is featured</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product category mapping </returns>
        public static ProductCategory UpdateProductCategory(int ProductCategoryID, int ProductID, int CategoryID,
           bool IsFeaturedProduct, int DisplayOrder)
        {
            ProductCategory productCategory = ProductCategoryDBManager.UpdateProductCategory(ProductCategoryID, ProductID, CategoryID,
            IsFeaturedProduct, DisplayOrder);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_ALLBYCATEGORYID_KEY);
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_ALLBYPRODUCTID_KEY);
                NopCache.RemoveByPattern(PRODUCTCATEGORIES_BY_ID_KEY);
            }
            return productCategory;
        }
        #endregion
    }
}
